package per.xue.carid_booking.utils;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import per.xue.carid_booking.entity.Car;
import per.xue.carid_booking.entity.Ticket;
import per.xue.carid_booking.service.CarService;
import per.xue.carid_booking.service.TicketService;

import java.util.Date;
import java.util.List;

/**
 * @auther zhang
 * @ TO DO
 * @creatTime 2021/5/23 17:10
 **/
@Slf4j
@Component
public class StateUpdateUtils {

    @Autowired
    private CarService carService;

    @Autowired
    private TicketService ticketService;

    @Scheduled(cron = "0 */1 * * * ?")
    public void statsUpdate() throws Exception {
        log.info("更新车次");
        List<Car> postCar = carService.getPostCar(new Date());
        for (Car car : postCar) {
            car.setStats("0");
            carService.update(car);
            log.info("车次"+car.getCarId()+"已发车");
            Ticket ticket = new Ticket();
            ticket.setCarId(car.getCarId());
            List<Ticket> ticketList = ticketService.findByParams(ticket);
            for (Ticket postTicket : ticketList) {
                //无论是否支付都变成过期
                if (postTicket.getStats()== TicketStateCode.RELEASE ||postTicket.getStats()==TicketStateCode.PAYED) {
                    postTicket.setStats(TicketStateCode.OVER_TIME);
                    ticketService.findByParams(postTicket);
                    log.info("票号"+postTicket.getTicketId()+"已过期");
                }
            }
        }
    }

}
